﻿#include <iostream>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <memory>
#include <ctime>

using namespace std;
#define MAP_STRESSING 512
#define ARRAY_STRESSING 512


typedef struct 
{
    int age;
    char sex;
    int a1;
    int a2;
    int a3;
    int a4;
    int a5;
    int a6;
    int a7;
    int a8;
}student_t,*pstudent_t;

typedef struct 
{
    int id;
    student_t students[ARRAY_STRESSING];
}school_t,*pschool_t;

void test_map();
void test_unordered_map();
void test_array_index();
void test_array_pointer();
void test_array_reference();

int main()
{
    clock_t time1=clock();
    clock_t time2=0;
    cout.setf(ios::fixed);
    cout<<"map测试：开始。"<<endl;
    test_map();
    time2=clock();
    cout<<"map测试："<<right<<setw(8)<<time2-time1<<"毫秒。"<<endl;
    cout<<"unordered_map测试：开始。"<<endl;
    time1=clock();
    test_unordered_map();
    time2=clock();
    cout<<"unordered_map测试："<<right<<setw(8)<<time2-time1<<"毫秒。"<<endl;
    cout<<"array_index测试：开始。"<<endl;
    time1=clock();
    test_array_index();
    time2=clock();
    cout<<"array_index测试："<<right<<setw(8)<<time2-time1<<"毫秒。"<<endl;
    cout<<"array_reference测试：开始。"<<endl;
    time1=clock();
    test_array_reference();
    time2=clock();
    cout<<"array_reference测试："<<right<<setw(8)<<time2-time1<<"毫秒。"<<endl;
    cout<<"array_pointer测试：开始。"<<endl;
    time1=clock();
    test_array_pointer();
    time2=clock();
    cout<<"array_pointer测试："<<right<<setw(8)<<time2-time1<<"毫秒。"<<endl;
    system("pause");
    return 0;
}

void test_map()
{
    map<int,map<int,student_t>> schools;
    for(int i=0;i<MAP_STRESSING;i++)
    {
        map<int,student_t> students;
        for(int j=0;j<MAP_STRESSING;j++)
        {
            students[j].age=j;
            students[j].sex='F';
            students[j].a1=j;
            students[j].a2=j;
            students[j].a3=j;
            students[j].a4=j;
            students[j].a5=j;
            students[j].a6=j;
            students[j].a7=j;
            students[j].a8=j;
        }
        schools[i]=students;
    }
}

void test_unordered_map()
{
    unordered_map<int,unordered_map<int,student_t>> schools;
    for(int i=0;i<MAP_STRESSING;i++)
    {
        unordered_map<int,student_t> students;
        for(int j=0;j<MAP_STRESSING;j++)
        {
            students[j].age=21;
            students[j].sex='F';
            students[j].a1=j;
            students[j].a2=j;
            students[j].a3=j;
            students[j].a4=j;
            students[j].a5=j;
            students[j].a6=j;
            students[j].a7=j;
            students[j].a8=j;
        }
        schools[i]=students;
    }
}

void test_array_index()
{
    shared_ptr<school_t> schools(new school_t[ARRAY_STRESSING]);
    pschool_t pschools=schools.get();
    for(int i=0;i<ARRAY_STRESSING;i++)
    {
        pschools[i].id=i;
        for(int j=0;j<ARRAY_STRESSING;j++)
        {
            pschools[i].students[j].age=j;
            pschools[i].students[j].sex='F';
            pschools[i].students[j].a1=j;
            pschools[i].students[j].a2=j;
            pschools[i].students[j].a3=j;
            pschools[i].students[j].a4=j;
            pschools[i].students[j].a5=j;
            pschools[i].students[j].a6=j;
            pschools[i].students[j].a7=j;
            pschools[i].students[j].a8=j;
        }
    }
}

void test_array_reference()
{
    shared_ptr<school_t> schools(new school_t[ARRAY_STRESSING]);
    pschool_t pschools=schools.get();
    school_t& obj1=pschools[0];
    student_t& obj2=pschools[0].students[0];
    for(int i=0;i<ARRAY_STRESSING;i++)
    {
        obj1=pschools[i];
        obj1.id=i;
        for(int j=0;j<ARRAY_STRESSING;j++)
        {
            obj2=obj1.students[j];
            obj2.age=j;
            obj2.sex='F';
            obj2.a1=j;
            obj2.a2=j;
            obj2.a3=j;
            obj2.a4=j;
            obj2.a5=j;
            obj2.a6=j;
            obj2.a7=j;
            obj2.a8=j;
        }
    }
}

void test_array_pointer()
{
    shared_ptr<school_t> schools(new school_t[ARRAY_STRESSING]);
    pschool_t pschools=schools.get();
    pschool_t pobj1=nullptr;
    pstudent_t pobj2=nullptr;
    for(int i=0;i<ARRAY_STRESSING;i++)
    {
        pobj1=&pschools[i];
        pobj1->id=i;
        for(int j=0;j<ARRAY_STRESSING;j++)
        {
            pobj2=&pobj1->students[j];
            pobj2->age=j;
            pobj2->sex='F';
            pobj2->a1=j;
            pobj2->a2=j;
            pobj2->a3=j;
            pobj2->a4=j;
            pobj2->a5=j;
            pobj2->a6=j;
            pobj2->a7=j;
            pobj2->a8=j;
        }
    }
}